package com.example.demo.service.impl;

import com.example.demo.dto.StudentCourseInfo;
import com.example.demo.entity.Student;
import com.example.demo.entity.StudentCourse;
import com.example.demo.mapper.StudentCourseMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;

    @Override
    public int insertStudent(Student student) {
        return studentMapper.insertStudent(student);
    }

    @Override
    public int deleteStudentById(int id) {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public int updateStudent(Student student) {
        return studentMapper.updateStudent(student);
    }

    @Override
    public Student selectStudentById(int id) {
        return studentMapper.selectStudentById(id);
    }

    @Override
    public List<Student> selectAllStudents() {
        return studentMapper.selectAllStudents();
    }

    @Override
    public Student selectStudentWithCourses(int studentId) {
        return studentMapper.selectStudentWithCourses(studentId);
    }

    @Override
    public List<StudentCourseInfo> getCoursesByStudentId(Integer studentId) {
        return studentMapper.getCoursesByStudentId(studentId);
    }

    @Override
    public void addCourse(int id) {
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setStudent_id(3);
        studentCourse.setCourse_id(id);
        studentCourse.setEnrolled_at(new Timestamp(System.currentTimeMillis()));
        studentCourseMapper.insertStudentCourse(studentCourse);
    }
}
